<!--  -->
<template>
  <div class='label-pop' v-if="makeTagPop">
    <van-popup v-model="makeTagPop" position="right" @close="downTagPop" :style="{ width: '100%' }" @opened="opened"
      ref="makeTagRef" :lock-scroll="false" :lazy-render="false">
      <div class="label-main">
        <van-nav-bar title="贴标签" left-arrow @click-left="downTagPop" />

        <div>
          <van-search v-model="groupName" show-action placeholder="请输入搜索关键词" shape="round" @input="labelSearch">
            <template #action>
              <div class="flex-a-c group-tag" @click.stop="confirmOn">
                <span class="flex-a-c "><span class="iconfont icon-life-game-plus"></span>添加标签</span>
              </div>
            </template>
          </van-search>
        </div>

        <div class="classify-content flex">
          <div class="classify-left" ref="groupTag">
              <div>
                <div class="left-box flex"  v-if="groupList.length">
                  <div class="group-box">

                    <div :class="classifyIndex === -1 ? 'on' : ''" @click="classifyOn(-1)">全部标签</div>
                  </div>
                  <block v-for="(item, index) in groupList" :key="index" class="group-box">
                    <div :class="classifyIndex === index ? 'on' : ''" @click="classifyOn(index, item)">{{ item.group_name }}
                    </div>
                  </block>
                </div>
                <span id="classifyMore" style="display: inline-block; width: 100%; height: 5rem;"></span>
              </div>
          </div>
  
          <div ref="handpickList" class="classify-main">
            <div>
              <div class="classify-box" v-if="tags && tags.length > 0">
                <div v-for="(item, index) in tags" :key="index">
                  <div class="group-title" :style="index == 0 ? 'margin-top:0;' : ''" v-if="item.group_name"> {{ item.group_name }}</div>
                  <div style="display: flex;flex-wrap: wrap;">
                    <div v-for="(c_item, c_index) in item.has_many_tag" :key="c_index" class="tags-box"
                    :class="(c_item.current_used || c_item.choose) ? 'tags-box-on' : ''"
                    @click="tabsClick(c_item, index, c_index)">
                    {{ c_item.name }}</div>
                  </div>
                </div>
              </div>
              <div  v-else style="padding-top: 10rem;">
                <img style="width: 7.1875rem;" :src="require('../../../assets/images/wechat_sidebar/2279.png')" />
                <div style="color:#6E6E79;font-size: .8125rem;margin-top: .8438rem;">暂无标签</div>
              </div>
              <span id="loadMore" style="display: inline-block; width: 100%; height: 5rem;"></span>
            </div>
          </div>
        </div>
      </div>
      <div class="footer-btn ">
        <div class="footer-checked">
          <div class="tags-checked " ref="tagsListRef">
            <div class="flex-a-c">
              <h3 style="flex-shrink: 0;font-weight: bold;">已选：</h3>
                <div style="display: flex;flex-wrap: wrap;align-items: center;">
                  <span class="tag-span"  v-for="(item, index) in tagsList"  :key="index"  @click="close_handle(item, index)">{{ item.name }}<i class="iconfont icon-guanbi"></i></span>
                </div>
            </div>
          </div>
        </div>
        <div class="flex-a-c flex-j-sb">
          <div class="footer-cancel" @click="clear">清除</div>
          <div class="footer-confirm" @click="confirm">确定</div>
        </div>
      </div>
    </van-popup>

  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';


export default {
  name: '',
  props: ['makeTagPop', 'tags', 'groupList', 'groupName', 'coThemeColor'],

  //import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    return {
      tags: [],
      keyWord: '',
      classifyIndex: -1,
      classify: [],
      classifyList: [],
      tagsList: [],
      test_arr:[]

    };
  },
  //方法集合
  methods: {
    clear() {
      this.tags.forEach(item => {
        item.has_many_tag.forEach(item => {
          item.choose = 0
        })
      })
      this.tags = this.tags;
      this.tagsList = [];
    },
    confirm() {
      let arr = this.tagsList.map(item => {
        return item.tag_id;
      })
      this.$emit('tabsClickConfirm', arr);
      this.tagsList = [];
    },
    downTagPop() {
      this.$emit('update:makeTagPop', this.makeTagPop = false);
    },
    labelSearch(val) {
      this.$emit('groupChange', val);
    },
    onLoadMore() {
      this.$parent.dividendScroll();
    },
    onLoadClassify() {
      this.$parent.groupScroll();
    },
    opened() {
      this.$parent.openedOn();
    },
    classifyOn(index, item = {}) {
      this.classifyIndex = index;
      this.tags = [];
      this.$emit('classifyOn', { index, item });
    },
    confirmOn() {
      this.$parent.confirmClick();
    },
    close_handle(item, index) {
      let _index = this.tags.findIndex(v => v.group_id == item.group_id)
      if (_index !== -1) {
        let chil_index = this.tags[_index].has_many_tag.findIndex(v => v.id == item.id)
        this.$set(this.tags[_index].has_many_tag[chil_index], 'choose', 0);
        this.tagsList.splice(index, 1);
      }

    },
    tabsClick(data, par_index, cur_index) {
      if (data.current_used) {
        this.$dialog.alert({ message: "已添加该标签" });
        return false;
      }
      let index = this.tagsList.findIndex(item => item.tag_id == data.tag_id);
      if (index == -1) {
        this.tagsList.push(data);
        this.$set(this.tags[par_index].has_many_tag[cur_index], 'choose', 1)
      } else {
        this.$set(this.tags[par_index].has_many_tag[cur_index], 'choose', 0)
        this.tagsList.splice(index, 1);
      }

    },

  }
}
</script>
<style scoped>
#appMain .pcStyle .van-popup {
  width: 100% !important;
}

.footer-checked ::v-deep .van-tag {
  padding: 6px 10px;
}
</style>
<style lang="scss" scoped>
::v-deep .van-nav-bar .van-icon{
  color:#00001C;
}
.icon-guanbi{
  font-size: 0.5rem;
  color:var(--themeBaseColor);
  margin-left: .25rem;
  line-height: 1;
}
.tag-span{
  cursor: pointer;
  padding: .4375rem .625rem;
  border-radius: .375rem;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: .5625rem;
  color: var(--themeBaseColor);
  margin-bottom: 0.5rem;
  z-index: 1;
  &::after{
    content:"";
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    background-color: var(--themeBaseColor);
    opacity: 0.1;
    border-radius: .375rem;
  }
}
.label-pop{
  position: fixed;
  left: 0;
  top: 0;
  bottom:0;
  right:0;
  z-index: 1110;
}
::v-deep .van-popup{
  height: 100%;
}
/* @import url(); 引入公共css类 */
.mb-65 {
  height: 65px;
  clear: both;
}

.group-tag {
  min-width: 25%;
  font-weight: 500;
  font-size: 13px;
  color: var(--themeBaseColor);

  .icon-life-game-plus {
    font-size: 13px;
  }

}

.label-titleBox {
  .classify-title {
    font-size: 14px;
    font-weight: bold;
    color: #1B1B1B;
    padding: 0 0 21px 0;
    width: 82px;
    text-align: center;
  }

  .tabs-title {
    font-weight: bold;
    font-size: 14px;
    padding: 0 0 20px 0;
    width: calc(100% - 16px);
    text-align: center;
  }
}

.mb-20 {
  height: 20px;
  clear: both;
}

.label-main {
  // padding:20px 0 0 0;
  position: relative;

  .label-title {
    border-left: 2px solid #f14e4e;
    font-size: 16px;
    line-height: 16px;
    font-weight: bold;
    text-align: left;
    padding: 0 0 0 10px;
    margin: 0 0 0 13px;
  }



  .classify-content {
    padding: 13px 0 0 0;
    position: relative;
    background-color: #F5F5F5;

    .classify-left {
      // background-color: #fff;

      height: 71vh;
      overflow: scroll;
      padding-bottom: 2rem;


      .left-box {
        width: 82px;
        // padding: 0 0 50px 0;
        // height: 87vh;
        // overflow-y: scroll;
        border-right: 1px solid #E9E5E5;
        flex-direction: column;

        div {
          // padding: 0 0 20px 0;
          text-align: center;
          font-size: 11px;
          color: #212121;
          display: inline-block;
          word-wrap: break-word;
          word-break: break-all;

          // padding:0 14px;
        }

        .group-box {
          padding: 0 0 32px 0;
          cursor: pointer;

          div {
            width: 100%;
            position: relative;

          }

          .on {
            display: inline-block;
            font-weight: bold;
            font-size: 14px;
            color: var(--themeBaseColor);

            &::after {
              content: '';
              width: 3px;
              height: 15px;
              // height: 100%;
              background-color: var(--themeBaseColor);
              position: absolute;
              left: 0;
              top: 50%;
              transform: translate(0, -50%);
            }
          }

          // line-height: 11px;

          // padding:5px 7px; 
          // border-radius: 11px;
          // background: #F78B00;
          // color: #FFFFFF;
          // position: relative;

          // margin: 0 0 23px 0;
        }
      }
    }

    .classify-main {
      box-sizing: border-box;
      width: calc(100% - 90px);
      height: 80vh;
      overflow: hidden;
      background-color: #fff;
      border-radius: 10px 10px 10px 10px;
      padding: 15px 10px 136px 10px;
      margin: 0 8px 0 0;
      position: relative;
      overflow-y: auto;

      .group-title {
        font-weight: 500;
        font-size: .875rem;
        color: #00001C;
        line-height: 1;
        text-align: left;
        margin-top: 1.25rem;
      }

      .tags-box-on {
        color: var(--themeBaseColor) !important;
        border: 1px solid var(--themeBaseColor) !important;
      }

      .tags-box {
        word-break: break-all;
        /* 在任意字符处断行 */
        word-wrap: break-word;
        margin: .625rem;
        margin-left: 0;
        margin-bottom: 0;
        border: 1px solid #D6D6DC;
        position: relative;
        font-size: 12px;
        color: #00001C;
        min-height: 34px;
        display: flex;
        align-items: center;
        justify-content: center;
        width: 7.9063rem;
        background: #FFFFFF;
        border-radius: .5rem;
      }

      .classify-box {
        // width:100%;
        padding: 15px 0 0 0;

        .goods-item {
          // flex-flow: row wrap;
          cursor: pointer;
          margin: 0 0 10px 0;
          max-width: 48%;
          flex: 0 0 48%;

          .on {
            color: var(--themeBaseColor);
            border: 1px solid var(--themeBaseColor);
          }
        }
      }
    }
  }
}

.footer-btn {
  background-color: #fff;
  box-shadow: 0 1px .3125rem 1px #D6D6DC;
  border-radius: .625rem .625rem 0 0;
  width: 100%;
  overflow: hidden;
  box-sizing: border-box;
  position: fixed;
  bottom: 0px;
  left: 0;
  padding: 12px 10px;

  .footer-checked {
    padding: 10px 0;
    text-align: left;

    h3 {
      font-weight: 500;
      font-size: 14px;
      color: #00001C;
      vertical-align: top;
      display: inline-block;
      padding: 0 2px 0 0;
    }

    .tag {
      margin: 0 9px 9px 0;
      border-radius: 6px 6px 6px 6px;
    }

    .tags-checked {
      height: auto;
      /* 确保高度根据内容自动调整 */
      max-height: 136px;
      /* 设置最大高度为四行的高度 */
      overflow-y: auto;
      /* 启用垂直滚动 */
      text-align: left;
      overflow: scroll;
      text-align: left;
    }
  }

  .footer-cancel {
    font-size: 14px;
    cursor: pointer;
    color: var(--themeBaseColor);
    width: calc((100% - 10px*2)/2);

    padding: 8px 0px;
    border: 1px solid var(--themeBaseColor);
    border-radius: 20px;
  }

  .footer-confirm {
    font-size: 14px;
    color: #FFFFFF;
    font-weight: 500;
    width: calc((100% - 10px*2)/2);
    background: var(--themeBaseColor);
    padding: 8px 0px;
    border-radius: 20px;
    cursor: pointer;
    // padding:12px 58px;
  }
}
</style>